package com.guchenbo.spark.stream

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * @author guchenbo
 * @date 2022/2/27
 */
object HelloStream {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("hello stream")
    conf.setMaster("local[2]")
    val ssc = new StreamingContext(conf, Seconds(3))
    ssc.sparkContext.setLogLevel("WARN")

    val lines = ssc.socketTextStream("localhost", 9999)

    val words = lines.flatMap(_.split(" "))

    val pairs = words.map(w => (w, 1))
    val wordCounts = pairs.reduceByKey((a, b) => a + b)
    wordCounts.print()

    ssc.start()
    ssc.awaitTermination()
  }
}
